﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using TrainingWorkshop.IDAL;

namespace TrainingWorkshop.DAL_SQL
{
    /// <summary>
    /// Author: Sissi Zeng
    /// Date:2012 08-08 15:00
    /// Read and Write from/into Table Teacher
    /// </summary>
    public class TeacherRepository_Sql:ITeacherRepository
    {
        private SqlConnection sqlConnection;

        public TeacherRepository_Sql(SqlConnection sqlConnection)
        {
            // TODO: Complete member initialization
            this.sqlConnection = sqlConnection;
        }







        public List<Model.Teacher> SelectAll()
        {
            List<Model.Teacher> teacherList = new List<Model.Teacher>();


            sqlConnection.Open();
            //get table
            string selectStatement = "select * from Teacher";
            SqlDataAdapter da = new SqlDataAdapter(selectStatement, sqlConnection);
            DataTable dt = new DataTable();
            da.Fill(dt);

            //convert to list
            foreach (DataRow row in dt.Rows)
            {
                Model.Teacher teacher = new Model.Teacher();
                teacher.TeacherId = Convert.ToInt32(row[0]);
                teacher.TeacherName = row[1].ToString().Trim();
                teacher.TeacherGender = row[2].ToString().Trim();
                teacher.DepartmentId = Convert.ToInt32(row[3]);
                teacherList.Add(teacher);
            }

            return teacherList;
        }
    }

}
